package com.dbs.donatebooksystem.Service.impl;

import com.dbs.donatebooksystem.Service.BooksService;
import com.dbs.donatebooksystem.mapper.booksMapper;
import com.dbs.donatebooksystem.pojo.Book;
import com.dbs.donatebooksystem.pojo.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
@Service
public class BooksServiceImpl implements BooksService {
    @Autowired
    private booksMapper booksMapper;

    @Override
    public boolean InsertBooks(Book book) {

        if(book.getBookname()==null||book.getPageCount()==0|| book.getUsages()==null){
            return false;
        }
        if(book.getTraceCode()==null){
            book.setTraceCode(book.generateRandomSixteenDigits());
            log.info("{}", book.getTraceCode());
        }


        /*if(book.getBooksCode() == null){
            book.setBooksCode(book.generateRandomBooksCode());
            log.info("{}", book.getBooksCode());
        }*/


        booksMapper.insert(book);


        Random r = new Random();
        int insert = booksMapper.getByName(book);
        Book bookNew = new Book();
        bookNew.setId(insert);
        bookNew.setBooksCode("" + (char) (r.nextInt(26) + 65) + insert);
        log.info("{}", bookNew.getBooksCode());
        booksMapper.updateById(bookNew);

        return true;
    }

    @Override
    public List<Book> SelectBooks(String bookname, String category) {

        return booksMapper.SelectBooks(bookname,category);
    }
    @Override
  public PageBean pageBean(Integer page,Integer pageSize,String bookname,String category){
      PageHelper.startPage(page,pageSize);
      List<Book> bookList =booksMapper.list(bookname,category);
      Page<Book> p = (Page<Book>) bookList;
      PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
      return pageBean;

  }



    @Override
    public List<List<Object>> selectCategoryNum() {
        List<Map<String, Object>> mapList = booksMapper.selectCategoryNum();
        List<Object> countsList = new ArrayList<>();
        List<Object> categoriesList = new ArrayList<>();

        for (Map<String, Object> map : mapList) {
            countsList.add(map.get("count"));
            categoriesList.add(map.get("category"));
        }

        // 将 countsList 和 categoriesList 添加到结果列表
        List<List<Object>> result = new ArrayList<>();
        result.add(countsList);
        result.add(categoriesList);

        return result;
    }

    @Override
    public void UpdateBook(Book book) {
        booksMapper.updateById(book);
    }

    @Override
    public void deleteBook(Integer id) {
        booksMapper.deleteById(id);
    }

    @Override
    public List<Book> SearchBooks(Book searchBook) {
        return booksMapper.searchBooks(searchBook);
    }

    @Override
    public Book selectBookById(Integer id) {
        return booksMapper.selectBookById(id);
    }


}
